var videoURL="http://www.youtube.com/v/TWfph3iNC-k";
var preload="0.05" //preload 5%
var videoContainer;
var gameStatus;
var ytplayer;		
var score=0;
var userinput="";
var keyboardEnabled=false;
var loadTimer;
var textbox;
var wordscoreElement;

function keyDown(event) {
  if(!event) var event=window.event;
  if(keyboardEnabled) {
    textbox.value=textbox.value.replace(" ","");
		if (event.keyCode==13) {
      userinput=textbox.value.trim();
      textbox.value="";
			
      if(userinput.length>0) {
        var request=new XMLHttpRequest();
        request.open("GET","/check?"+userId+";"+userinput+";"+Math.round(ytplayer.getCurrentTime()),false);
			
        request.send();
        wordscore=parseInt(request.responseText);
        score+=wordscore;
        document.getElementById("score").innerHTML=score;
			
        if(wordscore>0){wordscoreElement.style.color="#70f020";}
        else {wordscoreElement.style.color="orange";}
        animate(new Message("wordscore",[wordscore,"",wordscore,"",wordscore,""],2));
      
        userinput="";
      }
		} else if (event.keyCode==27) {
			userinput="";
			textbox.value="";
		}
  }
}
		
function onYouTubePlayerReady() { 
			ytplayer = document.getElementById("myytplayer");
			ytplayer.cueVideoByUrl(videoURL,0);
			ytplayer.playVideo();
			ytplayer.pauseVideo();
			ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
      videoContainer=document.getElementById("video_container");      
      gameStatus=document.getElementById("game_status");      
      loadTimer=setInterval("waitToLoad()",500);
      textbox=document.getElementById("textbox");
      textbox.onkeydown=keyDown;
      textbox.onblur=function(){if(keyboardEnabled){textbox.focus();}}
      wordscoreElement=document.getElementById("wordscore");
}

function waitToLoad() {
  progress=Math.round(ytplayer.getVideoBytesLoaded()/(ytplayer.getVideoBytesTotal()*preload)*100);  
  if(progress>=0) {gameStatus.innerHTML="Loading... "+progress+"%";}
  if(progress>=100) {
    clearInterval(loadTimer);
    gameStatus.innerHTML="GO!";
    keyboardEnabled=true;    
    ytplayer.playVideo();
    textbox.style.visibility="visible";
    textbox.focus();
    videoContainer.style.visibility="visible";
  }
}
		
function onPlayerStateChange(state){
			if(state==0) {
        keyboardEnabled=false;
				nickname=document.getElementById("nickname");
				nickname.value+=userId;
        textbox.style.visibility="hidden";
				namePopup=document.getElementById("namePopup");
				namePopup.style.visibility="visible";
        nickname.focus();
			}
}
		
function sendResults() {
			nickname=document.getElementById("nickname");

			var request=new XMLHttpRequest();
			request.onreadystatechange=function() {
				if (request.readyState==4) {
					window.location="gameover.jsp";
				}
			}
			request.open("GET","/check?"+userId+";;;"+score+";"+nickname.value,true);				
			request.send();	
}
		
function debug(text) {
			document.getElementById("debug").innerHTML=text;
}
		